<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-collapse ghost v-model:activeKey="activeKey">
			<a-collapse-panel key="1" header="数据配置">
				<a-row :gutter="[10, 10]">
					<a-col :span="24">
						<a-radio-group
							v-model:value="formState.dataSetConfig.dataSetType"
							optionType="button"
							buttonStyle="solid"
							:options="[
								{ label: '静态', value: 0 },
								{ label: '数据源', value: 1 }
							]"
						>
						</a-radio-group>
					</a-col>
					<a-col :span="24" v-if="formState.dataSetConfig.dataSetType === 0">
						<JsonEditor v-model="formState.data" />
					</a-col>
					<template v-else>
						<a-col :span="24">
							<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
						</a-col>
						<a-col :span="24">
							<a-form-item label="进度值字段">
								<a-select
									v-model:value="formState.data.percent.field"
									placeholder="请选择进度值字段"
									:options="fieldOptions"
									:fieldNames="{ label: 'name', value: 'field' }"
									@change="handleFieldChange"
								>
								</a-select>
							</a-form-item>
						</a-col>
					</template>
				</a-row>
			</a-collapse-panel>
		</a-collapse>
	</a-form>
</template>

<script setup>
	import config from '../config'
	import { defineEmits, defineProps, reactive, ref, watch } from 'vue'
	import { cloneDeep } from 'lodash-es'
	import { designStore } from '@/views/plugin/dashboard/store'
	import JsonEditor from '@/views/plugin/dashboard/plugin/public/JsonEditor/JsonEditor.vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'

	const useDesignStore = designStore()
	const emits = defineEmits(['upDataConfig'])
	const activeKey = ref(['1'])

	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return cloneDeep(config)
			}
		}
	})

	const formState = reactive(cloneDeep(props.config))

	// 数据源字段列表
	const fieldOptions = ref([])

	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.data.percent.field = undefined
		// 获取数据源字段
		if (formState.dataSetConfig.dataSetJson && Object.keys(formState.dataSetConfig.dataSetJson).length > 0) {
			datasetApi.preview(formState.dataSetConfig.dataSetJson).then((res) => {
				if (res && res.length > 0) {
					// 从返回的数据中提取字段
					const firstItem = res[0]
					fieldOptions.value = Object.keys(firstItem).map((field) => ({
						name: field,
						field: field
					}))
				}
			})
		}
	}

	// 字段变更处理
	const handleFieldChange = () => {
		// 如果需要在字段变更时执行特定逻辑，可以在这里添加
	}

	// 监听配置变化
	watch(
		() => formState,
		(val) => {
			emits('upDataConfig', cloneDeep({ ...val, ...formState }))
		},
		{ deep: true }
	)

	// 初始化时，如果已经选择了数据源，则获取字段列表
	watch(
		() => formState.dataSetConfig.dataSetJson,
		(newVal) => {
			if (newVal && Object.keys(newVal).length > 0 && formState.dataSetConfig.dataSetType === 1) {
				datasetApi.preview(newVal).then((res) => {
					if (res && res.length > 0) {
						const firstItem = res[0]
						fieldOptions.value = Object.keys(firstItem).map((field) => ({
							name: field,
							field: field
						}))
					}
				})
			}
		},
		{ immediate: true, deep: true }
	)
</script>

<style scoped>
	.form-item {
		margin-bottom: 0;
	}
</style>
